var Mobile = {};

Mobile.Effect = {
    speed: "fast"
}
Mobile.Date = {
    format:function(date,fmt)   
    { 
        var o = {   
            "M+": date.getMonth() + 1,                 //月份   
            "d+": date.getDate(),                    //日   
            "h+": date.getHours(),                   //小时   
            "m+": date.getMinutes(),                 //分   
            "s+": date.getSeconds(),                 //秒   
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
            "S": date.getMilliseconds()             //毫秒   
        };   
        if(/(y+)/.test(fmt))   
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for(var k in o)   
            if(new RegExp("("+ k +")").test(fmt))   
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
        return fmt;   
    },
    parseToDate:function(date) {//Date(1377828670436)
        return new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
    }
};
Mobile.Cookies = {
    get: function (name) {

        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));

        if (arr != null) return unescape(arr[2]); return null;
    },
    set: function (name, value, expires) {

        this.remove(name);

        var Days = expires || 7;

        var exp = new Date();

        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);

        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    },
    remove: function (name) {

        var exp = new Date();

        exp.setTime(exp.getTime() - 1);

        var cval = this.get(name);

        if (cval != null) {
            cval = '';
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
        }
    }
};
Mobile.Tip = {
    loading: function (text) {
        if (!text) {
            text = "加载中...";
        }
        var opts = {
            lines: 10, // The number of lines to draw
            length: 1, // The length of each line
            width: 4, // The line thickness
            radius: 17, // The radius of the inner circle
            corners: 1, // Corner roundness (0..1)
            rotate: 0, // The rotation offset
            color: '#FFF', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: false, // Whether to use hardware acceleration
            className: 'spinner', // The CSS class to assign to the spinner
            zIndex: 2e9, // The z-index (defaults to 2000000000)
            top: 'auto', // Top position relative to parent in px
            left: 'auto' // Left position relative to parent in px
        };
        var target = document.createElement("div");
        target.className="loading";
        document.body.appendChild(target);
        var spinner = new Spinner(opts).spin(target);
        var overlay = iosOverlay({
            text: text,
            //duration: 2e3,
            spinner: spinner
        });
    },
    Showloading: function (text) {
    	this.loading(text);
    },
    Hideloading: function () {
        $(".loading").next().remove();
        $(".loading").remove();
    },
    success: function (text) {
        if (!text) {
            text = "操作成功！";
        }
        iosOverlay({
            text: text,
            duration: 2e3,
            icon: "img/check.png"
        });
    },
    error: function (text) {
        if (!text) {
            text = "操作失败！";
        }
        iosOverlay({
            text: text,
            duration: 2e3,
            icon: "img/cross.png"
        });
    }
};
Mobile.SweetAlert = {
	alert:function(option){
        var title = option.title ? option.title : "???";
        var text  = option.text  ? option.text  : "???";
        swal({
            title:title, 
            text:text,
            html: true
        })
    },
    success:function(){
		swal("Good!", "弹出了一个操作成功的提示框", "success");
	},
    error:function(){
        swal("OMG!", "弹出了一个操作成功的提示框", "error");
    },
    confirm:function(option){
        var title = option.title ? option.title : "???";
        var text  = option.text  ? option.text  : "???";
        var cancelButtonText  = option.cancelButtonText  ? option.cancelButtonText  : "取消";//closeOnConfirm
        var confirmButtonText = option.confirmButtonText ? option.confirmButtonText : "确定";//confirmButtonText
        //showCancelButton: true, 
        //closeOnConfirm: false,
        swal({
            title: title,
            text: text,
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: confirmButtonText,
            cancelButtonText:cancelButtonText
            //showCancelButton: true, 
            //closeOnConfirm: false,
        },
        function(){
            if (option.callback && jQuery.isFunction(option.callback)) {
                option.callback();
            }
        });
    },
    custom:function(){
        swal({
            title: "Good!",
            text: '自定义<span style="color:red">图片</span>、<a href="#">HTML内容</a>。<br/>3秒后自动关闭。',
            imageUrl: "img/thumbs-up.jpg",
            html: true,
            timer: 3000,
            showConfirmButton: false
        });
    },
    transport:function(){
        swal({
            title: "输入框来了",
            text: "这里可以输入并确认:",
            type: "input",
            showCancelButton: true,
            closeOnConfirm: false,
            animation: "slide-from-top",
            inputPlaceholder: "填点东西到这里面吧"
            }, 
            function(inputValue) {
                if (inputValue === false)
                    return false;
                if (inputValue === "") {
                    swal.showInputError("请输入!");
                    return false
            }
            swal("棒极了!", "您填写的是: " + inputValue, "success");
        });
    }
}
Mobile.Ajax = {
    loadText: "请稍等...",
    ajaxService: function (url, data, callback, dataType, isAsyn, needFailCallback) {
        if (!dataType) {
            dataType = "json";
        }
        if (isAsyn == undefined || isAsyn == null) {
            isAsyn = true;
        }
        Mobile.Tip.Showloading(this.loadText);
        $.ajax({
            type: "GET",
            url: url,
            data: data,
            async: isAsyn,
            dataType: dataType,
            success: function (data) {
                Mobile.Tip.Hideloading();
                callback(data);
                // if (data.Success) {
                //     if (callback) {
                //         callback(data);
                //     }
                // } else {
                //     if (needFailCallback && callback) {
                //         callback(data);
                //     }
                //     else {
                //         console.log("data.Success=false,原因：", data);
                //         Mobile.Tip.error(data.ErrorMsg);
                //     }
                // }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                Mobile.Tip.Hideloading();
                console.log("Ajax出错：", textStatus + "-" + XMLHttpRequest);
            }
        });
    },
    /***
    url:发送请求地址。
    data:待发送 Key/value 参数。
    callback:发送成功时回调函数。
    type:返回内容格式，xml, html, script, json, text, _default。
    ***/
    post: function (url, data, callback, type) {
        $.post(url, data, callback, type);
    }
};
